package com.chukun.flink.dataset.operator;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple1;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * @author chukun
 * @version 1.0.0
 * @description flatmap 操作符的基本操作
 * @createTime 2022年05月28日 20:35:00
 */
public class FlatMapOperator {

    public static void main(String[] args) throws Exception {

        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        DataSet<Tuple2<String, Integer>> dataSet = env.fromElements(
                new Tuple2<>("hello dataset", 1),
                new Tuple2<>("spark", 2),
                new Tuple2<>("hello flink", 2));

        DataSet<Tuple1<String>> flatMappedData = dataSet.flatMap(new FlatMapFunction<Tuple2<String,Integer>, Tuple1<String>>() {
            @Override
            public void flatMap(Tuple2<String, Integer> tuple2, Collector<Tuple1<String>> collector) throws Exception {
                if ("spark".equals(tuple2.f0)) {
                    return;
                }
                collector.collect(Tuple1.of("not.inclued.spark :"+tuple2.f0));
            }
        });

        flatMappedData.print("FlatMapFunction");

        env.execute("FlatMapOperator");
    }
}
